<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div class="product-detail">
    <s-header :name="'商品详情'"></s-header>
    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <img :src="state.detail.cover_img" style="width: 100%; height:10rem;" alt="" />
      </div>
      <div class="product-info">
        <div class="product-title">
          {{ state.detail.title || "" }}
        </div>
        <div class="product-desc">公司：{{ state.detail.company_name }}</div>
        <div class="product-desc" v-if="state.detail.type + '' === '0'">类型：需求</div>
        <div class="product-desc" v-else-if="state.detail.type + '' === '1'">类型：商品</div>
        <div class="product-desc">地址：{{ state.detail.city }}</div>
        <div class="product-price">
          <span v-if="state.detail.salary_up">¥{{ state.detail.salary_down || "" }}~{{
              state.detail.salary_up || ""
            }}</span
          >
          <span v-else>¥{{ state.detail.salary_down || "" }}</span>
          <!-- <span>库存203</span> -->
        </div>
      </div>
      <div class="product-intro">
        <ul>
          <li>浏览量</li>
          <li>{{ state.detail.hits }}</li>
          <li>数量</li>
          <li>{{ state.detail.quantity }}</li>
        </ul>
        <!-- <div
          class="product-content"
          v-html="state.detail.details || ''"
        ></div> -->
        <iframe id="iframe" src="" width="100%" frameborder="0" />
      </div>
    </div>
    <!-- <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" />
      <van-action-bar-icon
        icon="cart-o"
        :badge="!cart.count ? '' : cart.count"
        @click="goTo()"
        text="购物车"
      />
      <van-action-bar-button
        type="warning"
        @click="handleAddCart"
        text="加入购物车"
      />
      <van-action-bar-button type="danger" @click="goToCart" text="立即购买" />
    </van-action-bar> -->
  </div>
</template>

<script setup>
import { reactive, onMounted, nextTick } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useCartStore } from "@/stores/cart";
import { getDetail, updateHits } from "@/service/good";
import { addCart } from "@/service/cart";
import sHeader from "@/components/SimpleHeader.vue";
import { showSuccessToast } from "vant";
import { prefix } from "@/common/js/utils";
const route = useRoute();
const router = useRouter();
const cart = useCartStore();

const state = reactive({
  detail: {
    goodsCarouselList: [],
  },
});

onMounted(async () => {
  const { id } = route.params;

  await updateHits(id);
  const data = await getDetail(id);
  console.log(123123123, data);
  // data.goodsCarouselList = data.goodsCarouselList.map(i => prefix(i))
  state.detail = data;

  let iframe = document.getElementById("iframe");
  iframe.contentWindow.document.body.innerHTML = data.details || "";
  setIframeHeight(iframe);
  // cart.updateCart()
});

nextTick(() => {
  // 一些和DOM有关的东西
  const content = document.querySelector(".detail-content");
  content.scrollTop = 0;
});

const goBack = () => {
  router.go(-1);
};

const goTo = () => {
  router.push({ path: "/cart" });
};

const handleAddCart = async () => {
  const { resultCode } = await addCart({
    goodsCount: 1,
    goodsId: state.detail.goodsId,
  });
  if (resultCode == 200) showSuccessToast("添加成功");
  cart.updateCart();
};

const goToCart = async () => {
  await addCart({ goodsCount: 1, goodsId: state.detail.goodsId });
  cart.updateCart();
  router.push({ path: "/cart" });
};

const setIframeHeight = (iframe) => {
  if (iframe) {
    var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
    if (iframeWin.document.body) {
      iframe.height =
        iframeWin.document.documentElement.scrollHeight ||
        iframeWin.document.body.scrollHeight;
    }
  }
};
</script>

<style lang="less">
@import "../common/style/mixin";
.product-detail {
  .detail-header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10000;
    .fj();
    .wh(100%, 44px);
    line-height: 44px;
    padding: 0 10px;
    .boxSizing();
    color: #252525;
    background: #fff;
    border-bottom: 1px solid #dcdcdc;
    .product-name {
      font-size: 14px;
    }
  }
  .detail-content {
    height: calc(100vh - 50px);
    overflow: hidden;
    overflow-y: auto;
    .detail-swipe-wrap {
      .my-swipe .van-swipe-item {
        img {
          width: 100%;
          // height: 300px;
        }
      }
    }
    .product-info {
      padding: 0 10px;
      .product-title {
        font-size: 18px;
        text-align: left;
        color: #333;
      }
      .product-desc {
        font-size: 14px;
        text-align: left;
        color: #999;
        padding: 5px 0;
      }
      .product-price {
        .fj();
        span:nth-child(1) {
          color: #f63515;
          font-size: 22px;
        }
        span:nth-child(2) {
          color: #999;
          font-size: 16px;
        }
      }
    }
    .product-intro {
      width: 100%;
      padding-bottom: 50px;
      ul {
        .fj();
        width: 100%;
        margin: 10px 0;
        li {
          flex: 1;
          padding: 5px 0;
          text-align: center;
          font-size: 15px;
          border-right: 1px solid #999;
          box-sizing: border-box;
          &:last-child {
            border-right: none;
          }
        }
      }
      .product-content {
        padding: 0 20px;
        img {
          width: 100%;
        }
      }
    }
  }
  .van-action-bar-button--warning {
    background: linear-gradient(to right, #6bd8d8, @primary);
  }
  .van-action-bar-button--danger {
    background: linear-gradient(to right, #0dc3c3, #098888);
  }
}
</style>
